package com.sunday.shoufen.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.common.widgets.adapter.CommonAdapter;
import com.sunday.common.widgets.adapter.ViewHolder;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.shoufen.R;
import com.sunday.shoufen.adapter.CommentAdapter;
import com.sunday.shoufen.base.BaseApp;
import com.sunday.shoufen.entity.CashRecord;
import com.sunday.shoufen.entity.Comment;
import com.sunday.shoufen.http.ApiClient;

import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.sunday.common.widgets.EmptyLayout.HIDE_LAYOUT;
import static com.sunday.common.widgets.EmptyLayout.NODATA;
import static com.sunday.shoufen.R.id.bank_name;

/**
 * Created by 刘涛 on 2017/11/8.
 */

public class CashRecordActivity extends BaseActivity {


    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;


    private CommonAdapter<CashRecord> adapter;
    private LinearLayoutManager layoutManager;
    private List<CashRecord> dataSet = new ArrayList<>();
    private int pageNo = 1, lastVisibleItem;
    private boolean isCanloadMore;
    private long memberId;

    private int cashType = 1;//提现类型 1-消费积分分红提现 2-怀亚积分分红提现 3-商家营收提现

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refresh_list);
        ButterKnife.bind(this);
        cashType = getIntent().getIntExtra("cashType", 1);
        initView();
        getData();
    }

    private void initView() {
        titleView.setText(cashType == 1 ? "积分奖励兑换记录" : "提现记录");
        memberId = BaseApp.getInstance().getMember().getId();
        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new CommonAdapter<CashRecord>(mContext, R.layout.list_cash_record, dataSet) {
            @Override
            public void convert(ViewHolder holder, CashRecord cashRecord) {
                bind(holder, cashRecord);
            }
        };
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext).drawable(R.drawable.shape_divider_width).build());
        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (isCanloadMore) {
                    pageNo++;
                    getData();
                } else {
                    refreshlayout.finishLoadmore();
                }
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNo = 1;
                getData();
            }
        });
        emptyLayout.setErrorType(HIDE_LAYOUT);
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pageNo = 1;
                getData();
            }
        });


    }

    private void bind(ViewHolder holder, CashRecord cashRecord) {
        TextView money = holder.getView(R.id.money);
        TextView time = holder.getView(R.id.time);
        TextView bankName = holder.getView(bank_name);
        TextView status = holder.getView(R.id.status);
        money.setText(cashRecord.getAmount().setScale(2, RoundingMode.HALF_UP) + "");
        bankName.setText(TextUtils.isEmpty(cashRecord.getCard_no()) ? "怀亚积分兑换记录" : "卡号：" + cashRecord.getCard_no());
        status.setText(cashRecord.getStatus() == 0 ? "审核中" : cashRecord.getStatus() == 1 ? "已完成" : "未通过");
        time.setText(cashRecord.getCreate_time());


    }


    private void getData() {
        showLoadingDialog(0);
        Call<ResultDO<List<CashRecord>>> call = ApiClient.getApiAdapter().cashRecord(memberId, 1, cashType, pageNo, Constants.PAGE_SIZE);
        call.enqueue(new Callback<ResultDO<List<CashRecord>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<CashRecord>>> call, Response<ResultDO<List<CashRecord>>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                dissMissDialog();
                refreshLayout.finishRefresh();
                if (response.body().getCode() == 0) {
                    if (pageNo == 1) {
                        dataSet.clear();
                    }
                    dataSet.addAll(response.body().getResult());
                    if (response.body().getResult().size() == Constants.PAGE_SIZE) {
                        isCanloadMore = true;
                    } else {
                        isCanloadMore = false;
                    }
                    emptyLayout.setErrorType(dataSet.size() > 0 ? HIDE_LAYOUT : NODATA);
                    adapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<CashRecord>>> call, Throwable t) {
                dissMissDialog();
                refreshLayout.finishRefresh();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

}
